<?php

namespace App\Store\Models\Account;

use App\Admin\Models\Brand;
use App\Admin\Models\BusinessMem;
use App\Admin\Models\CustomerServiceMem;
use App\Admin\Models\SiteCompany;
use App\Common\Services\Common;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use App\Common\Services\Store\Common as StoreCommon;

class SiteStore extends Model
{
    protected $table = 'site_store_list';
    public $timestamps = false;
    protected $primaryKey = 'id';
    protected $guarded = ['_token'];


    /**
     *获取列表
     */
    public static function getList(Request $request)
    {
        $accountinfo = StoreCommon::getCurrentAccountinfo($request);
        $cp_id = $accountinfo['cp_id'];
        $is_all_store =$request->is_all_store??0;
        $store_ids = is_array($accountinfo['store_ids']) ? $accountinfo['store_ids'] : (empty($accountinfo['store_ids'])?0:[$accountinfo['store_ids']]);
        $query = self::with(['companyinfo', 'brandinfo', 'businessmeminfo', 'customerserviceinfo'])
            ->where(['audit_status' => 3, 'cp_id' => $cp_id])
            ->when(!empty($store_ids)&& $is_all_store==0, function ($query) use ($store_ids) {
                $query->whereIn('id', $store_ids);
            })
            ->where('is_del', 1)
            ->orderBy('create_at', 'desc');
        $ids = $request->ids;
        $ids = is_array($ids) ? $ids : (is_array(json_decode($ids, true)) ? json_decode($ids, true) : []);
        if (count($ids) > 0) {
            $query->whereIn('id', $ids);
        }

        $isgetall = intval($request->isgetall) ?? 0;
        $p_code = $request->p_code;
        if ($p_code != '') {
            $query->where('p_code', $p_code);
        }

        $is_can_use = $request->is_can_use;
        if (in_array($is_can_use, [1, 2])) {
            $query->where('is_can_use', $is_can_use);
        }

        $c_code = $request->c_code;
        if ($c_code != '') {
            $query->where('c_code', $c_code);
        }

        $d_code = $request->d_code;
        if ($d_code != '') {
            $query->where('d_code', $d_code);
        }

        $store_name = $request->store_name;
        if ($store_name != '') {
            $query->where('store_name', 'like', '%' . $store_name . '%');
        }
        $business_mem_name = $request->business_mem_name;
        if ($business_mem_name != '') {
            $query->where('business_mem_name', 'like', '%' . $business_mem_name . '%');
        }


        $withoutcurrent = intval($request->withoutcurrent) ?? 0;

        if ($withoutcurrent == 1) {//不包含当前店铺
            $store_ids = StoreCommon::getCurrentAccountStoreId($request);

            if ($store_ids) { //权限控制
                if (is_numeric($store_ids) && intval($store_ids) > 0) { //获取当前门店下的列表
                    $query->where('id', '<>', intval($store_ids));
                }
            }
        }


        $start_time = strtotime($request->start_time) > 0 ? strtotime($request->start_time) : intval($request->start_time);
        $end_time = strtotime($request->end_time) > 0 ? strtotime($request->end_time) : intval($request->end_time);
        if ($start_time > 0 && $end_time > 0) {
            $query->where('create_at', '>=', $start_time)->where('create_at', '<=', $end_time);
        }

        if ($isgetall == 0) {
            $limit = $request->limit ?? 10;
            $list = $query->paginate($limit)->toArray();
            return Common::paginateDataToArray($list);
        } else {
            $list = $query->get()->toArray();
            return ['list' => $list];
        }
    }

    public function adminuserinfo()
    {
        return $this->hasOne(Account::class, 'id', 'admin_user_id');
    }

    public function companyinfo()
    {
        return $this->hasOne(SiteCompany::class, 'id', 'cp_id');
    }


    public function brandinfo()
    {
        return $this->hasOne(Brand::class, 'id', 'brand_id');
    }

    public function businessmeminfo()
    {
        return $this->hasOne(BusinessMem::class, 'id', 'business_mem_id');
    }

    public function customerserviceinfo()
    {
        return $this->hasOne(CustomerServiceMem::class, 'id', 'customer_service_id');
    }

}
